package com.example.shiwu.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.example.shiwu.R;
import com.example.shiwu.SendReceiveDetailsActivity;
import com.example.shiwu.db.vo.ReceiveVo;
import com.example.shiwu.db.vo.SendVo;

import java.util.ArrayList;
import java.util.List;

public class SendReceiveAddPoPAdapter extends BaseAdapter {

    private Context mContext;
    private int resourceId;
    public boolean flage = false;
    private List<ReceiveVo> listItems = new ArrayList<ReceiveVo>();
    private LayoutInflater mInflater;


    public SendReceiveAddPoPAdapter(Context mContext, List<ReceiveVo> listItems, int resource) {
        this.resourceId = resource;
        this.mContext = mContext;
        this.listItems = listItems;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        final ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(parent.getContext()).inflate(resourceId, parent, false);
            viewHolder = new ViewHolder(view, position);
            viewHolder.device_id.setText(listItems.get(position).getDeviceId());
            viewHolder.send_items_name.setText(listItems.get(position).getName());
            viewHolder.send_items_voltage.setText(listItems.get(position).getVoltage());
//            viewHolder.send_items_electricity.setText(listItems.get(position).getElectricity());
//            viewHolder.send_item_location.setText(listItems.get(position).getSendLocal());
//            viewHolder.receive_item_location.setText(listItems.get(position).getReceiveLocal());
            //viewHolder.send_device_type.setText(listItems.get(position).getDeviceType());
            viewHolder.send_device_type.setText("");
            //viewHolder.send_operator.setText(String.valueOf(listItems.get(position).getOperator()));
            viewHolder.send_items_createTime.setText(listItems.get(position).getCreateTime());
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (SendReceiveAddPoPAdapter.ViewHolder) view.getTag();
        }
        viewHolder.device_id.setText(listItems.get(position).getDeviceId());
        viewHolder.send_items_name.setText(listItems.get(position).getName());
        viewHolder.send_items_voltage.setText(listItems.get(position).getVoltage());
//            viewHolder.send_items_electricity.setText(listItems.get(position).getElectricity());
//            viewHolder.send_item_location.setText(listItems.get(position).getSendLocal());
//            viewHolder.receive_item_location.setText(listItems.get(position).getReceiveLocal());
        //viewHolder.send_device_type.setText(listItems.get(position).getDeviceType());
        viewHolder.send_device_type.setText("");
        //viewHolder.send_operator.setText(String.valueOf(listItems.get(position).getOperator()));
        viewHolder.send_items_createTime.setText(listItems.get(position).getCreateTime());

        viewHolder.detailsSub.setOnClickListener(new View.OnClickListener() {//详情
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,SendReceiveDetailsActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                mContext.startActivity(intent);
            }
        });

        viewHolder.delSb.setOnClickListener(new View.OnClickListener() {//删除
            @Override
            public void onClick(View v) {
                System.out.println("id =================================== " + v.getId());
                //点击删除按钮之后，给出dialog提示
                AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                builder.setTitle("确认删除?");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        System.out.println("id =================================== " + position);

                        Toast.makeText(v.getContext(), "删除成功！", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.show();
            }
        });
        //viewHolder.checkboxOperateData = (CheckBox) convertView.findViewById(R.id.device_items_checkbox);
        // 根据isSelected来设置checkbox的显示状况
//        if (flage) {
//            holder.checkboxOperateData.setVisibility(View.VISIBLE);
//        } else {
//            holder.checkboxOperateData.setVisibility(View.GONE);
//        }

        return view;
    }

    @Override
    public int getCount() {
        return listItems.size();
    }

    @Override
    public Object getItem(int position) {
        return listItems.get(position);
    }


    @Override
    public long getItemId(int position) {
        return position;
    }


    class ViewHolder {

        public TextView detailsSub,delSb, device_id,send_items_name,send_items_voltage,send_device_type,send_operator,send_items_createTime;
//        public TextView detailsSub,delSb, device_id,send_items_name, send_items_voltage, send_items_electricity,
//                send_item_location,receive_item_location,send_device_type,send_operator,send_items_createTime;
        public CheckBox send_checkbox;

        public ViewHolder(View view, int position) {
            device_id = view.findViewById(R.id.device_id_val);
            send_items_name = view.findViewById(R.id.send_items_name_val);
            send_items_voltage = view.findViewById(R.id.send_items_voltage_val);
//            send_items_electricity = view.findViewById(R.id.send_items_electricity_val);
//            send_item_location = view.findViewById(R.id.send_item_location_val);
//            receive_item_location = view.findViewById(R.id.receive_item_location_val);
            send_device_type = view.findViewById(R.id.send_device_type_val);
            send_operator = view.findViewById(R.id.send_operator_val);
            send_items_createTime = view.findViewById(R.id.send_items_createTime_val);
            detailsSub = view.findViewById(R.id.send_items_details_sb);
            delSb = view.findViewById(R.id.send_items_del_sb);
            //send_checkbox = view.findViewById(R.id.send_items_checkbox);
        }

    }

}
